package controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.bd.excepcion.DAOException;
import modelo.servicio.Responsables;

public class ResponsableControlador extends HttpServlet {

    private String accion;
    private String usuario;
    private StringBuilder html;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        html = new StringBuilder("");
        accion = request.getParameter("ac") != null
                ? request.getParameter("ac") : "";
        usuario = request.getSession().getAttribute("sUsuario") != null
                ? request.getSession().getAttribute("sUsuario").toString() : "";
        try {
            if (accion.compareToIgnoreCase("ls") == 0) {
            } else if (accion.compareToIgnoreCase("sv") == 0) {
                String claveFlujo = request.getParameter("clvf") != null
                        ? request.getParameter("clvf") : "";
                String clavePaso = request.getParameter("clvp") != null
                        ? request.getParameter("clvp") : "";
                String claveRes = request.getParameter("cmb_usuario") != null
                        ? request.getParameter("cmb_usuario") : "";
                String forzoso = request.getParameter("ckb_forzoso") != null
                        ? request.getParameter("ckb_forzoso") : "";
                String aprobador = request.getParameter("ckb_aprobador") != null
                        ? request.getParameter("ckb_aprobador") : "";
                String recibeCopia = request.getParameter("ckb_recibe_copia") != null
                        ? request.getParameter("ckb_recibe_copia") : "";
                Responsables.instancia().agregar(claveFlujo, clavePaso,
                        forzoso.equalsIgnoreCase("ON"), claveRes, aprobador.equalsIgnoreCase("ON"),
                        recibeCopia.equalsIgnoreCase("ON"), usuario);
                html.append("<script type='text/javascript'>")
                        .append("charge('#ui-tabs-4', 'front?v=10&ac=1&clvf=")
                        .append(claveFlujo.replace("'", "\\'")).append("&clvp=")
                        .append(clavePaso.replace("'", "\\'")).append("');")
                        .append("</script>");
            } else if (accion.compareToIgnoreCase("ed") == 0) {
                String claveFlujo = request.getParameter("clvf") != null
                        ? request.getParameter("clvf") : "";
                String clavePaso = request.getParameter("clvp") != null
                        ? request.getParameter("clvp") : "";
                String claveRes = request.getParameter("cmb_usuario") != null
                        ? request.getParameter("cmb_usuario") : "";
                String forzoso = request.getParameter("ckb_forzoso") != null
                        ? request.getParameter("ckb_forzoso") : "";
                String aprobador = request.getParameter("ckb_aprobador") != null
                        ? request.getParameter("ckb_aprobador") : "";
                String recibeCopia = request.getParameter("ckb_recibe_copia") != null
                        ? request.getParameter("ckb_recibe_copia") : "";
                Responsables.instancia().editar(claveFlujo, clavePaso,
                        forzoso.equalsIgnoreCase("ON"), claveRes, aprobador.equalsIgnoreCase("ON"),
                        recibeCopia.equalsIgnoreCase("ON"), usuario);
                html.append("<script type='text/javascript'>")
                        .append("charge('#ui-tabs-4', 'front?v=10&ac=1&clvf=")
                        .append(claveFlujo.replace("'", "\\'")).append("&clvp=")
                        .append(clavePaso.replace("'", "\\'")).append("');")
                        .append("</script>");
            } else if (accion.compareToIgnoreCase("el") == 0) {
                String claveFlujo = request.getParameter("clvf") != null
                        ? request.getParameter("clvf") : "";
                String clavePaso = request.getParameter("clvp") != null
                        ? request.getParameter("clvp") : "";
                String claveRes = request.getParameter("cmb_usuario") != null
                        ? request.getParameter("cmb_usuario") : "";
                Responsables.instancia().eliminar(claveFlujo, clavePaso, claveRes, usuario);
                html.append("<script type='text/javascript'>")
                        .append("charge('#ui-tabs-4', 'front?v=10&ac=1&clvf=")
                        .append(claveFlujo.replace("'", "\\'")).append("&clvp=")
                        .append(clavePaso.replace("'", "\\'")).append("');")
                        .append("</script>");
            }
            out.println(html);
        } catch (DAOException dex) {
            out.println("Error en la base de datos: " + dex.getMessage() + "<br/>" + dex.getLocalizedMessage());
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
